// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DC.Markets.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.Forge.DataCache
{
    #region Using Directives

    using System;
    using System.Collections.Generic;
    using System.Linq;

    using Org.OpenTrader.Framework.Forge.Entities;
    using Org.OpenTrader.Framework.Forge.Interfaces;

    #endregion

    /// <summary>
    /// The dao.
    /// </summary>
    public partial class DC
    {
        #region Constants and Fields

        /// <summary>
        /// Markets cache
        /// </summary>
        private readonly IDictionary<Guid, IMarket> marketsCache = new Dictionary<Guid, IMarket>();

        /// <summary>
        /// True if the cache has been fully loaded (GetMarketsFromCache called once)
        /// </summary>
        private bool marketsCacheFullyLoaded;

        #endregion

        #region Public Methods

        /// <summary>
        /// The create market.
        /// </summary>
        /// <param name="dictionaryId">
        /// The dictionaryId.
        /// </param>
        /// <param name="path">
        /// The path.
        /// </param>
        /// <returns>
        /// </returns>
        public IMarket CreateMarketAndCache(Guid dictionaryId, string path)
        {
            var market = DBMarketHelpers.CreateMarket(dictionaryId, path);
            this.marketsCache[market.ID] = market;
            return market;
        }

        /// <summary>
        /// The load marketsCache.
        /// </summary>
        /// <param name="dictionaryId">
        /// The dictionaryId.
        /// </param>
        /// <returns>
        /// </returns>
        public IMarket[] GetMarketsFromCache(Guid dictionaryId)
        {
            // If the cache is not yet fully loaded, load it
            if (!this.marketsCacheFullyLoaded)
            {
                // Fill the cache
                foreach (var m in DBMarketHelpers.LoadMarkets(dictionaryId))
                {
                    this.marketsCache[m.ID] = m;
                }

                // FieldCache is fully loaded
                this.marketsCacheFullyLoaded = true;
            }

            // Return the cache as array
            return this.marketsCache.Values.ToArray();
        }

        #endregion
    }
}